﻿using System;
using System.Text;
using Unknown.Framework.Database;
using Unknown.Example.Entity.Personnel;
using Unknown.Example.Sql.Personnel;

namespace Unknown.Example.OracleSql.Personnel
{
	/// <summary>
	/// 职员Oracle SQL抽象类
	/// </summary>
	public abstract class EmployeeOracleSql : IEmployeeSql
	{
		/// <summary>
		/// 构建新增
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>操作</returns>
		public virtual Operation BuildInsert(Employee entity)
		{
			Operation result = new Operation();

			if (entity != null)
			{
				NoQueryOperationHandler operationHandler = new NoQueryOperationHandler();

				result.Sql = @"
INSERT INTO P_EMPLOYEE T
  (T.PKID,
   T.INSERT_OPERATOR,
   T.INSERT_TIME,
   T.UPDATE_OPERATOR,
   T.UPDATE_TIME,
   T.VALIDITY,
   T.INTEGER_SERIAL,
   T.TEXT_SERIAL,
   T.REMARK,
   T.VERIFY_KEY,
   T.VERIFY_VALUE,
   T.NAME)
VALUES
  (:PKID,
   :INSERT_OPERATOR,
   :INSERT_TIME,
   :UPDATE_OPERATOR,
   :UPDATE_TIME,
   :VALIDITY,
   :INTEGER_SERIAL,
   :TEXT_SERIAL,
   :REMARK,
   :VERIFY_KEY,
   :VERIFY_VALUE,
   :NAME)
";

				result.Parameters.Add(new Parameter("PKID", entity.Pkid, "Varchar2"));
				result.Parameters.Add(new Parameter("INSERT_OPERATOR", entity.InsertOperator, "Varchar2"));
				result.Parameters.Add(new Parameter("INSERT_TIME", entity.InsertTime, "Date"));
				result.Parameters.Add(new Parameter("UPDATE_OPERATOR", entity.UpdateOperator, "Varchar2"));
				result.Parameters.Add(new Parameter("UPDATE_TIME", entity.UpdateTime, "Date"));
				result.Parameters.Add(new Parameter("VALIDITY", entity.Validity, "Int32"));
				result.Parameters.Add(new Parameter("INTEGER_SERIAL", entity.IntegerSerial, "Int64"));
				result.Parameters.Add(new Parameter("TEXT_SERIAL", entity.TextSerial, "Varchar2"));
				result.Parameters.Add(new Parameter("REMARK", entity.Remark, "Clob"));
				result.Parameters.Add(new Parameter("VERIFY_KEY", entity.VerifyKey, "Varchar2"));
				result.Parameters.Add(new Parameter("VERIFY_VALUE", entity.VerifyValue, "Varchar2"));
				result.Parameters.Add(new Parameter("NAME", entity.Name, "Varchar2"));

				result.OperationHandler = operationHandler;
			}

			return result;
		}

		/// <summary>
		/// 构建修改
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>操作</returns>
		public virtual Operation BuildUpdate(Employee entity)
		{
			Operation result = new Operation();

			if (entity != null)
			{
				NoQueryOperationHandler operationHandler = new NoQueryOperationHandler();

				result.Sql = @"
UPDATE P_EMPLOYEE T
   SET T.PKID = :PKID,
       T.UPDATE_OPERATOR = :UPDATE_OPERATOR,
       T.UPDATE_TIME = :UPDATE_TIME,
       T.VALIDITY = :VALIDITY,
       T.INTEGER_SERIAL = :INTEGER_SERIAL,
       T.TEXT_SERIAL = :TEXT_SERIAL,
       T.REMARK = :REMARK,
       T.VERIFY_KEY = :VERIFY_KEY,
       T.VERIFY_VALUE = :VERIFY_VALUE,
       T.NAME = :NAME
 WHERE T.PKID = :PKID
";

				result.Parameters.Add(new Parameter("PKID", entity.Pkid, "Varchar2"));


				result.Parameters.Add(new Parameter("UPDATE_OPERATOR", entity.UpdateOperator, "Varchar2"));
				result.Parameters.Add(new Parameter("UPDATE_TIME", entity.UpdateTime, "Date"));
				result.Parameters.Add(new Parameter("VALIDITY", entity.Validity, "Int32"));
				result.Parameters.Add(new Parameter("INTEGER_SERIAL", entity.IntegerSerial, "Int64"));
				result.Parameters.Add(new Parameter("TEXT_SERIAL", entity.TextSerial, "Varchar2"));
				result.Parameters.Add(new Parameter("REMARK", entity.Remark, "Clob"));
				result.Parameters.Add(new Parameter("VERIFY_KEY", entity.VerifyKey, "Varchar2"));
				result.Parameters.Add(new Parameter("VERIFY_VALUE", entity.VerifyValue, "Varchar2"));
				result.Parameters.Add(new Parameter("NAME", entity.Name, "Varchar2"));
				result.Parameters.Add(new Parameter("PKID", entity.Pkid, "Varchar2"));
				
				result.OperationHandler = operationHandler;
			}

			return result;
		}

		/// <summary>
		/// 构建删除
		/// </summary>
		/// <param name="pkid">标识</param>
		/// <returns>操作</returns>
		public virtual Operation BuildDelete(string pkid)
		{
			Operation result = new Operation();

			NoQueryOperationHandler operationHandler = new NoQueryOperationHandler();

			result.Sql = @"
DELETE FROM P_EMPLOYEE T WHERE T.PKID = :PKID
";
			result.Parameters.Add(new Parameter("PKID", pkid, "Varchar2"));
			result.OperationHandler = operationHandler;

			return result;
		}

		/// <summary>
		/// 构建查询
		/// </summary>
		/// <param name="first">开始行</param>
		/// <param name="last">结束行</param>
		/// <returns>操作</returns>
		public virtual Operation BuildSelect(int first, int last)
		{
			Operation result = new Operation();

			EmployeeOperationHandler operationHandler = new EmployeeOperationHandler();
			operationHandler.First = first;
			operationHandler.Last = last;

			result.Sql = @"
SELECT T.PKID AS T_PKID,
       T.INSERT_OPERATOR AS T_INSERT_OPERATOR,
       T.INSERT_TIME AS T_INSERT_TIME,
       T.UPDATE_OPERATOR AS T_UPDATE_OPERATOR,
       T.UPDATE_TIME AS T_UPDATE_TIME,
       T.VALIDITY AS T_VALIDITY,
       T.INTEGER_SERIAL AS T_INTEGER_SERIAL,
       T.TEXT_SERIAL AS T_TEXT_SERIAL,
       T.REMARK AS T_REMARK,
       T.VERIFY_KEY AS T_VERIFY_KEY,
       T.VERIFY_VALUE AS T_VERIFY_VALUE,
       T.NAME AS T_NAME
  FROM P_EMPLOYEE T
";
			result.OperationHandler = operationHandler;

			return result;
		}

		/// <summary>
		/// 构建查询
		/// </summary>
		/// <param name="pkid">标识</param>
		/// <returns>操作</returns>
		public virtual Operation BuildSelect(string pkid)
		{
			Operation result = new Operation();

			result = this.BuildSelect(1, int.MaxValue);

			StringBuilder sqlBuilder = new StringBuilder();
			sqlBuilder.Append(result.Sql);

			string where = @"
WHERE T.PKID = :PKID
";
			sqlBuilder.Append(where);
		result.Parameters.Add(new Parameter("PKID", pkid, "Varchar2"));

			result.Sql = sqlBuilder.ToString();

			return result;
		}

		/// <summary>
		/// 构建查询
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns>操作</returns>
		public virtual Operation BuildSelectUnique(Employee entity)
		{
			return null;
		}
	}
}
